package com.konne.nightmare.FastPublicOpinion.ui.information.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpFragment;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.SelectSectionPresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.view.ISelectSectionView;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;
import com.konne.nightmare.FastPublicOpinion.widget.QJCheckBox;
import com.konne.nightmare.FastPublicOpinion.widget.QJItemTitleView;

import java.util.List;

import butterknife.BindView;

/**
 * Create by ws.
 * on Data:2022/2/11  Time:下午 2:26.
 * description: ** .
 */
public class SelectSectionFragment extends BaseMvpFragment<ISelectSectionView, SelectSectionPresenter> implements ISelectSectionView {
    @BindView(R.id.ll_content)
    LinearLayout ll_content;

    public static SelectSectionFragment getInstance() {
        return new SelectSectionFragment();
    }

    @Override
    protected SelectSectionPresenter createPresenter() {
        return new SelectSectionPresenter();
    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return null;
    }

    @Override
    public void cancelLoadDialog() {

    }

    @Override
    public int getLayout() {
        return R.layout.frag_select_section;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mPresenter.init();
    }

    @Override
    public void updataSection(List<SelectSectionPresenter.Section> datas) {
        ll_content.removeAllViews();
        for (int i = 0; i < datas.size(); i++) {
            ll_content.addView(getSectionView(datas.get(i),i==datas.size()-1));
        }
    }

    private View getSectionView(SelectSectionPresenter.Section data, boolean isLast) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_section, null);
        QJCheckBox checkBox=view.findViewById(R.id.checkbox);
        view.findViewById(R.id.cl_item).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkBox.setChecked(!checkBox.isChecked());
            }
        });
        ImageView iv_arrow = view.findViewById(R.id.iv_arrow);

        TextView tv_sec_num = view.findViewById(R.id.tv_sec_num);
        tv_sec_num.setText(data.sNum);
        TextView tv_sec_title = view.findViewById(R.id.tv_sec_title);
        tv_sec_title.setText(data.sTitle);
        LinearLayout ll_content = view.findViewById(R.id.ll_content);
        ll_content.removeAllViews();
        for (int i = 0; i < data.beats.size(); i++) {
            ll_content.addView(getBeatView(data.beats.get(i)));
        }
        if (!isLast) {
            View line = new View(getActivity());
            line.setBackgroundColor(0xffF6F6F6);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(10));
            params.topMargin=SizeUtils.dp2px(10);
            line.setLayoutParams(params);
            ll_content.addView(line);
        }
        iv_arrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iv_arrow.setSelected(!iv_arrow.isSelected());
                iv_arrow.setImageResource(iv_arrow.isSelected()?R.mipmap.icon_circle_arrow_gray_up:R.mipmap.icon_circle_arrow_gray_down);
                ll_content.setVisibility(iv_arrow.isSelected()?View.GONE:View.VISIBLE);
            }
        });
        return view;
    }

    private View getBeatView(String data) {
        QJItemTitleView titleView = new QJItemTitleView(getActivity());
        titleView.setTitle("    "+data);
        titleView.setTitleSize(14);
        titleView.setTitleColor(0xff344854);
        titleView.setShowLine(true);
        titleView.setStyle(QJItemTitleView.Style.check);
        titleView.getQjCheckBox().setStyle(QJCheckBox.Style.circle);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(50));
        params.leftMargin=SizeUtils.dp2px(30);
        params.rightMargin=SizeUtils.dp2px(15);
        titleView.setLayoutParams(params);
        titleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                titleView.getQjCheckBox().setChecked(! titleView.getQjCheckBox().isChecked());
            }
        });
        return titleView;
    }
}
